/*
	YoghurtGum - 2D Game Engine

	Copyright 2009-2011 Quinten Lansu
	
	Licensed under the Apache License, Version 2.0 (the "License"); you may not 
	use this file except in compliance with the License. You may obtain a copy 
	of the License at 
		
		http://www.apache.org/licenses/LICENSE-2.0 
		
	Unless required by applicable law or agreed to in writing, software 
	distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
	License for the specific language governing permissions and limitations 
	under the License. 
*/

#ifndef _RENDERMETHODOPENGLES_H_
#define _RENDERMETHODOPENGLES_H_

#include "YoghurtGum.h"
#include "YGRenderMethod.h"
#include "YGRenderMethodManager.h"
#include "YGRoot.h"
#include "YGFileManager.h"

#include "SourcesOpenGLES.h"
#include "TextureOpenGLES.h"

namespace YoghurtGum
{

	class ViewportOpenGLES;
	class SpriteOpenGLES;

	class FileStreamZip;

	class RenderMethodOpenGLES : public RenderMethod
	{

	public:

		RenderMethodOpenGLES();
		~RenderMethodOpenGLES();

		static RenderMethodOpenGLES* Get();

		Surface* GetRenderTarget();

		unsigned long GetFillColor(Color a_Color);

		bool Init();
		void Resize(int a_Width, int a_Height);
		void CleanUp();

		void SetClearColor(Color a_Color);
		void SetFullScreen();

		char* GetName();

		bool Clear();
		bool PreRender();
		bool PostRender();

		bool LoadImage(const char* a_File, const char* a_Name, Color a_AlphaKey = Color::CNONE);

		Sprite* CreateSprite(const char* a_Name);
		SpriteEx* CreateSpriteEx(const char* a_Name, uint32 a_Flags = YG_SPRITEEX_DEFAULT);
		Background* CreateBackground(const char* a_Name, uint32 a_Flags = YG_BACKGROUND_DEFAULT);
		Surface* CreateSurface(int a_Width, int a_Height, uint32 a_Flags = YG_SURFACE_DEFAULT);

		uint32 CreateViewport(uint32 a_Width, uint32 a_Height, uint32 a_Type = YG_VIEWPORT_DEFAULT);
		Viewport* GetViewport(uint32 a_ID);
		bool SetViewport(uint32 a_ID);

		bool NonPowerOfTwoTex();
		TextureOpenGLES* GetTexture();

	private:

		static RenderMethodOpenGLES* s_Instance;

		struct DictionaryEntry
		{
			til::Image* image;
			char name[256];
			Color key;
			DictionaryEntry* next;
		};
		DictionaryEntry* m_ListFirst;
		DictionaryEntry* m_ListCurrent;

		struct SpriteEntry
		{
			SpriteOpenGLES* sprite;
			SpriteEntry* next;
		};
		SpriteEntry* m_SpriteFirst;
		SpriteEntry* m_SpriteCurrent;

		struct TextureEntry
		{
			TextureOpenGLES* tex;
			TextureEntry* next;
		};
		TextureEntry* m_TextureFirst;
		TextureEntry* m_TextureCurrent;

		DictionaryEntry* FindImage(const char* a_Name);

		GLclampf m_Clear[4];

		FileStreamZip* m_Stream;

		ViewportOpenGLES*     m_ViewportDefault;
		ViewportOpenGLES*     m_ViewportList[256];
		uint32                m_ViewportTotal;
		ViewportOpenGLES*     m_ViewportCurrent;

		bool m_NonPowerOfTwo;

	}; // class RenderMethodOpenGLES

}; // namespace YoghurtGum

#endif